﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace TangoFamilyAndGuide.DB
{
    public class Housing : db
    {
        public enum eHorarios
		{
			None,
			DB,
			Lista
		};
        List<Entidades.HorarioFecha> horarios = new List<Entidades.HorarioFecha>();

        public Housing(Entidades.Sesion Sesion) : base(Sesion)
        {
        }
        public List<Entidades.Housing> LeerListaHousingsPorUsuario()
        {
            List<Entidades.Housing> lista = new List<Entidades.Housing>();
            if (sesion.Usuario.Id != null)
            {
                StringBuilder a = new StringBuilder(string.Empty);
                a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
                a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
                a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
                a.Append("from Housing where Housing.IdUsuario in (Select Permiso.IdUsuario from Permiso where Permiso.IdTipoPermiso='OperHousing' and Permiso.IdUsuario='" + sesion.Usuario.Id + "' and Permiso.Estado='Active') ");
                DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
                if (dt.Rows.Count != 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Entidades.Housing Housing = new Entidades.Housing();
                        Housing.Id = Convert.ToInt32(dt.Rows[i]["IdHousing"]);
                        Copiar(dt.Rows[i], Housing);
                        lista.Add(Housing);
                    }
                }
            }
            return lista;
        }
        public void Leer(Entidades.Housing Housing)
        {
            StringBuilder a = new StringBuilder(string.Empty);
            a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
            a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
            a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
            a.Append("from Housing ");
            a.Append("where Housing.IdHousing = " + Housing.Id + " ");
            DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
            if (dt.Rows.Count == 0)
            {
                throw new TangoFamilyAndGuide.EX.Validaciones.ElementoInexistente("Housing " + Housing.Id);
            }
            else
            {
                Copiar(dt.Rows[0], Housing);
            }
        }
       
        private void Copiar(DataRow Desde, Entidades.Housing Hasta)
        {
            Hasta.Id = Convert.ToInt32(Desde["IdHousing"]);
            Hasta.Usuario.Id = Convert.ToString(Desde["IdUsuario"]);
            Hasta.Descripcion = Convert.ToString(Desde["Descripcion"]);
            Hasta.Pais = Convert.ToString(Desde["Pais"]);
            Hasta.Provincia = Convert.ToString(Desde["Provincia"]);
            Hasta.Localidad = Convert.ToString(Desde["Localidad"]);
            Hasta.Direccion = Convert.ToString(Desde["Direccion"]);
            Hasta.CodPost = Convert.ToString(Desde["CodPost"]);
            Hasta.Contacto.Nombre = Convert.ToString(Desde["NombreContacto"]);
            Hasta.Contacto.Email = Convert.ToString(Desde["EmailContacto"]);
            Hasta.Contacto.Telefono = Convert.ToString(Desde["TelefonoContacto"]);
            Hasta.Comentarios = Convert.ToString(Desde["Comentarios"]);
            Hasta.GoogleMap = Convert.ToString(Desde["GoogleMap"]);
            Hasta.Ranking = Convert.ToInt32(Desde["Ranking"]);
            Hasta.WF.Id = Convert.ToInt32(Desde["IdWF"]);
            Hasta.WF.Estado = Convert.ToString(Desde["Estado"]);
            Hasta.UltActualiz = ByteArray2TimeStamp((byte[])Desde["UltActualiz"]);
            Hasta.TipoDestacado = Convert.ToString(Desde["TipoDestacado"]);
            Hasta.Neighborhood = Convert.ToString(Desde["Neighborhood"]);
            Hasta.ApproximateAddress = Convert.ToString(Desde["ApproximateAddress"]);
            Hasta.SizeSgm = Convert.ToString(Desde["SizeSgm"]);
            Hasta.TypePlace = Convert.ToString(Desde["TypePlace"]);
            Hasta.TypeRoom = Convert.ToString(Desde["TypeRoom"]);
            Hasta.TypeBathroom = Convert.ToString(Desde["TypeBathroom"]);
            Hasta.HousingOffer = Convert.ToString(Desde["HousingOffer"]);
            Hasta.SkypeAddress = Convert.ToString(Desde["SkypeAddress"]);
            Hasta.Moneda = Convert.ToString(Desde["Moneda"]);
            if (!(String.IsNullOrEmpty(Desde["PricePerDay"].ToString())))
            {
                Hasta.PricePerDay = Convert.ToDecimal(Desde["PricePerDay"]);
            }
            if (!(String.IsNullOrEmpty(Desde["PricePerMonth"].ToString())))
            {
                Hasta.PricePerMonth = Convert.ToDecimal(Desde["PricePerMonth"]);
            }
            Hasta.PriceComment = Convert.ToString(Desde["PriceComment"]);
            Hasta.Servicios = Convert.ToString(Desde["Servicios"]);
            Hasta.NombreImagenPortada = "PortadaGral.jpg";
        }
        public int Crear(Entidades.Housing Housing)
        {
            StringBuilder a = new StringBuilder(string.Empty);
            a.AppendLine("declare @idWF varchar(256) ");
            a.AppendLine("declare @idUltimoIdHousing varchar(256) ");
            a.AppendLine("update Configuracion set @idWF=Valor=convert(varchar(256), convert(int, Valor)+1) where IdItemConfig='UltimoIdWF' ");
            a.AppendLine("update Configuracion set @idUltimoIdHousing=Valor=convert(varchar(256), convert(int, Valor)+1) where IdItemConfig='UltimoIdHousing' ");
            a.Append("Insert Housing (IdHousing, IdUsuario, Descripcion, Pais, Provincia, Localidad, Direccion, CodPost, ");
            a.Append("NombreContacto, EmailContacto, TelefonoContacto, Comentarios, GoogleMap, IdWF, Estado, Ranking, TipoDestacado, Neighborhood, ApproximateAddress, SizeSgm, TypePlace, TypeRoom, TypeBathroom, HousingOffer, SkypeAddress, Moneda, PricePerDay, PricePerMonth, PriceComment, Servicios) values (");
            a.Append("@idUltimoIdHousing, ");
            a.Append("'" + Housing.Usuario.Id + "', ");
            a.Append("'" + Housing.Descripcion + "', ");
            a.Append("'" + Housing.Pais + "', ");
            a.Append("'" + Housing.Provincia + "', ");
            a.Append("'" + Housing.Localidad + "', ");
            a.Append("'" + Housing.Direccion + "', ");
            a.Append("'" + Housing.CodPost + "', ");
            a.Append("'" + Housing.Contacto.Nombre + "', ");
            a.Append("'" + Housing.Contacto.Email + "', ");
            a.Append("'" + Housing.Contacto.Telefono + "', ");
            a.Append("'" + Housing.Comentarios + "', ");
            a.Append("'" + Housing.GoogleMap + "', ");
            a.Append("@idWF, ");
            a.Append("'" + Housing.WF.Estado + "', ");
            a.Append(Housing.Ranking + ", ");
            a.Append("'" + Housing.TipoDestacado + "', ");
            a.Append("'" + Housing.Neighborhood + "', ");
            a.Append("'" + Housing.ApproximateAddress + "', ");
            a.Append("'" + Housing.SizeSgm + "', ");
            a.Append("'" + Housing.TypePlace + "', ");
            a.Append("'" + Housing.TypeRoom + "', ");
            a.Append("'" + Housing.TypeBathroom + "', ");
            a.Append("'" + Housing.HousingOffer + "', ");
            a.Append("'" + Housing.SkypeAddress + "', ");
            a.Append("'" + Housing.Moneda + "', ");
            a.Append(Housing.PricePerDay + ", ");
            a.Append(Housing.PricePerMonth + ", ");
            a.Append("'" + Housing.PriceComment + "', ");
            a.Append("'" + Housing.Servicios + "' ");
            a.AppendLine(") ");
            a.AppendLine("insert Log values (@idWF, getdate(), '" + sesion.Usuario.Id + "', 'Housing', 'New', '" + Housing.WF.Estado + "', '') ");
            a.AppendLine("Select @idUltimoIdHousing");
            a.AppendLine();
            DataTable dt = new DataTable();
            dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.Usa, sesion.CnnStr);
            int resp = 0;
            if (dt.Rows.Count != 0)
            {
                resp = Convert.ToInt32(dt.Rows[0][0].ToString());
            }
            return resp;
        }
        public void Modificar(Entidades.Housing Desde, Entidades.Housing Hasta)
        {
            if (sesion.Usuario.Id != null)
            {
                StringBuilder a = new StringBuilder(string.Empty);
                a.Append("update Housing set ");
                a.Append("IdUsuario = '" + Hasta.Usuario.Id + "', ");
                a.Append("Descripcion = '" + Hasta.Descripcion + "', ");
                a.Append("Pais = '" + Hasta.Pais + "', ");
                a.Append("Provincia = '" + Hasta.Provincia + "', ");
                a.Append("Localidad = '" + Hasta.Localidad + "', ");
                a.Append("Direccion = '" + Hasta.Direccion + "', ");
                a.Append("CodPost = '" + Hasta.CodPost + "', ");
                a.Append("NombreContacto = '" + Hasta.Contacto.Nombre + "', ");
                a.Append("EmailContacto = '" + Hasta.Contacto.Email + "', ");
                a.Append("TelefonoContacto = '" + Hasta.Contacto.Telefono + "', ");
                a.Append("Comentarios = '" + Hasta.Comentarios + "', ");
                a.Append("GoogleMap = '" + Hasta.GoogleMap + "', ");
                a.Append("Ranking = '" + Hasta.Ranking + "', ");
                a.Append("TipoDestacado = '" + Hasta.TipoDestacado + "', ");
                a.Append("Neighborhood = '" + Hasta.TipoDestacado + "', ");
                a.Append("ApproximateAddress = '" + Hasta.ApproximateAddress + "', ");
                a.Append("SizeSgm = '" + Hasta.SizeSgm + "', ");
                a.Append("TypePlace = '" + Hasta.TypePlace + "', ");
                a.Append("TypeRoom = '" + Hasta.TypeRoom + "', ");
                a.Append("TypeBathroom = '" + Hasta.TypeBathroom + "', ");
                a.Append("HousingOffer = '" + Hasta.HousingOffer + "', ");
                a.Append("SkypeAddress = '" + Hasta.SkypeAddress + "', ");
                a.Append("Moneda = '" + Hasta.Moneda + "', ");
                a.Append("PricePerDay = " + Hasta.PricePerDay + ", ");
                a.Append("PricePerMonth = " + Hasta.PricePerMonth + ", ");
                a.Append("PriceComment = '" + Hasta.PriceComment + "', ");
                a.Append("Servicios = '" + Hasta.Servicios + "' ");
                a.Append("where IdHousing = " + Hasta.Id + " ");
                a.AppendLine("delete HorarioFecha where NombreEntidad='Housing' and IdEntidad = " + Hasta.Id + " ");
                a.AppendLine("insert Log values (" + Hasta.WF.Id.ToString() + ", getdate(), '" + sesion.Usuario.Id + "', 'Housing', 'Modif', '" + Hasta.WF.Estado + "', '') ");
                a.AppendLine("declare @idLog int ");
                a.AppendLine("select @idLog=@@Identity ");
                a.AppendLine("insert LogDetalle (IdLog, TipoDetalle, Detalle) values (@idLog, 'Desde', '" + Funciones.ObjetoSerializado(Desde) + "')");
                a.AppendLine("insert LogDetalle (IdLog, TipoDetalle, Detalle) values (@idLog, 'Hasta', '" + Funciones.ObjetoSerializado(Hasta) + "')");
                Ejecutar(a.ToString(), TipoRetorno.None, Transaccion.Usa, sesion.CnnStr);
            }
        }
        public void CambiarEstado(Entidades.Housing Housing, string Evento, string Estado)
        {
            if (sesion.Usuario.Id != null)
            {
                StringBuilder a = new StringBuilder(string.Empty);
                a.Append("update Housing set ");
                a.Append("Estado='" + Estado + "' ");
                a.AppendLine("where IdHousing=" + Housing.Id);
                a.AppendLine("insert Log values (" + Housing.WF.Id.ToString() + ", getdate(), '" + sesion.Usuario.Id + "', 'Housing', '" + Evento + "', '" + Estado + "', '') ");
                Ejecutar(a.ToString(), TipoRetorno.None, Transaccion.Usa, sesion.CnnStr);
            }
        }
        public List<Entidades.Housing> ListaPorIdHousing(int IdHousing, bool SoloPropias)
        {
            List<Entidades.Housing> lista = new List<Entidades.Housing>();
            if (sesion.Usuario.Id != null)
            {
                StringBuilder a = new StringBuilder(string.Empty);
                a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
                a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
                a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
                a.Append("from Housing where (Housing.IdUsuario in (Select Permiso.IdUsuario from Permiso where Permiso.IdTipoPermiso='OperHousing' and Permiso.IdUsuario='" + sesion.Usuario.Id + "' and Permiso.Estado='Active')");
                if (!SoloPropias)
                {
                    a.Append(" or '" + sesion.Usuario.Id + "' in (Select Permiso.IdUsuario from Permiso where Permiso.IdTipoPermiso='AdminSITE' and Permiso.IdUsuario='" + sesion.Usuario.Id + "' and Permiso.Estado='Active')) ");
                }
                else
                {
                    a.Append(") ");
                }
                a.Append("and IdHousing = " + IdHousing);
                DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
                if (dt.Rows.Count != 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Entidades.Housing Housing = new Entidades.Housing();
                        Copiar(dt.Rows[i], Housing);
                        lista.Add(Housing);
                    }
                }
            }
            return lista;
        }
        public List<Entidades.Housing> ListaPorDireccion(string Direccion, bool SoloPropias)
        {
            List<Entidades.Housing> lista = new List<Entidades.Housing>();
            if (sesion.Usuario.Id != null)
            {
                if (sesion.Usuario.Id != null)
                {
                    StringBuilder a = new StringBuilder(string.Empty);
                    a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
                    a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
                    a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
                    a.Append("from Housing where (Housing.IdUsuario in (Select Permiso.IdUsuario from Permiso where Permiso.IdTipoPermiso='OperHousing' and Permiso.IdUsuario='" + sesion.Usuario.Id + "' and Permiso.Estado='Active')");
                    if (!SoloPropias)
                    {
                        a.Append(" or '" + sesion.Usuario.Id + "' in (Select Permiso.IdUsuario from Permiso where Permiso.IdTipoPermiso='AdminSITE' and Permiso.IdUsuario='" + sesion.Usuario.Id + "' and Permiso.Estado='Active')) ");
                    }
                    else
                    {
                        a.Append(") ");
                    }
                    a.Append("and Direccion like '%" + Direccion + "%'");
                    DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
                    if (dt.Rows.Count != 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            Entidades.Housing Housing = new Entidades.Housing();
                            Copiar(dt.Rows[i], Housing);
                            lista.Add(Housing);
                        }
                    }
                }
            }
            return lista;
        }
        public List<Entidades.Housing> ListaPorPais(string Pais, bool SoloPropias)
        {
            List<Entidades.Housing> lista = new List<Entidades.Housing>();
            if (sesion.Usuario.Id != null)
            {
                StringBuilder a = new StringBuilder(string.Empty);
                a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
                a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
                a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
                a.Append("from Housing where (Housing.IdUsuario in (Select Permiso.IdUsuario from Permiso where Permiso.IdTipoPermiso='OperHousing' and Permiso.IdUsuario='" + sesion.Usuario.Id + "' and Permiso.Estado='Active')");
                if (!SoloPropias)
                {
                    a.Append(" or '" + sesion.Usuario.Id + "' in (Select Permiso.IdUsuario from Permiso where Permiso.IdTipoPermiso='AdminSITE' and Permiso.IdUsuario='" + sesion.Usuario.Id + "' and Permiso.Estado='Active')) ");
                }
                else
                {
                    a.Append(") ");
                }
                a.Append("and Pais like '%" + Pais + "%'");
                DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
                if (dt.Rows.Count != 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Entidades.Housing Housing = new Entidades.Housing();
                        Copiar(dt.Rows[i], Housing);
                        lista.Add(Housing);
                    }
                }
            }
            return lista;
        }
        public List<Entidades.Housing> ListaCompleta(string OrderBy, string Direccion, string Pais, string Provincia, string Localidad, string Estado)
        {
            List<Entidades.Housing> lista = new List<Entidades.Housing>();
            StringBuilder a = new StringBuilder(string.Empty);
            a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
            a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
            a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
            a.Append("from Housing where 1=1 ");
            if (!Direccion.Equals(string.Empty))
            {
                a.Append("and Direccion like '%" + Direccion + "%' ");
            }
            if (!Pais.Equals(string.Empty))
            {
                a.Append("and Pais like '%" + Pais + "%' ");
            }
            if (!Provincia.Equals(string.Empty))
            {
                a.Append("and Provincia like '%" + Provincia + "%' ");
            }
            if (!Localidad.Equals(string.Empty))
            {
                a.Append("and Localidad like '%" + Localidad + "%' ");
            }
            if (!Estado.Equals(string.Empty))
            {
                if (!Estado.Equals("All"))
                {
                    a.Append("and Estado like '%" + Estado + "%' ");
                }
            }
            else
            {
                a.Append("and Estado = 'Active' or Estado = 'Reserved' ");
            }
            a.Append("order by " + OrderBy);
            DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Entidades.Housing Housing = new Entidades.Housing();
                    Copiar(dt.Rows[i], Housing);
                    lista.Add(Housing);
                }
            }
            return lista;
        }
        public List<Entidades.Housing> Lista(int IndicePagina, int TamañoPagina, string OrderBy, string SessionID, List<Entidades.Housing> HousingLista)
        {
            System.Text.StringBuilder a = new StringBuilder();
            a.Append("CREATE TABLE #Housing" + SessionID + "( ");
            a.Append("[IdHousing] [int] NOT NULL, ");
	        a.Append("[IdUsuario] [varchar](50) NOT NULL, ");
	        a.Append("[Descripcion] [varchar](250) NOT NULL, ");
	        a.Append("[Pais] [varchar](50) NOT NULL, ");
	        a.Append("[Provincia] [varchar](50) NOT NULL, ");
	        a.Append("[Localidad] [varchar](50) NOT NULL, ");
	        a.Append("[Direccion] [varchar](60) NOT NULL, ");
	        a.Append("[CodPost] [varchar](10) NOT NULL, ");
	        a.Append("[NombreContacto] [varchar](25) NOT NULL, ");
	        a.Append("[EmailContacto] [varchar](60) NOT NULL, ");
	        a.Append("[TelefonoContacto] [varchar](50) NOT NULL, ");
            a.Append("[Comentarios] [varchar](250) NOT NULL, ");
            a.Append("[GoogleMap] [varchar](100) NOT NULL, ");
            a.Append("[IdWF] [int] NOT NULL, ");
            a.Append("[Estado] [varchar](15) NOT NULL, ");
            a.Append("[Ranking] [int] NOT NULL, ");
            a.Append("[TipoDestacado] [varchar](2) NOT NULL, ");
            a.Append("[Neighborhood] varchar(50) not null, ");
            a.Append("[ApproximateAddress] varchar(60) not null, ");
            a.Append("[SizeSgm] int not null, ");
            a.Append("[TypePlace] varchar(50) not null, ");
            a.Append("[TypeRoom] varchar(50) not null, ");
            a.Append("[TypeBathroom] varchar(50) not null, ");
            a.Append("[HousingOffer] varchar(50) not null, ");
            a.Append("[SkypeAddress] varchar(100) not null, ");
            a.Append("[Moneda] varchar(10) not null, ");
            a.Append("[PricePerDay] [numeric](18, 2) NULL, ");
            a.Append("[PricePerMonth] [numeric](18, 2) NULL, ");
            a.Append("[PriceComment] varchar(250) not null, ");
            a.Append("[Servicios] varchar(500) not null, ");
            a.Append("CONSTRAINT [PK_Housing" + SessionID + "] PRIMARY KEY CLUSTERED ");
            a.Append("( ");
            a.Append("[IdHousing] ASC ");
            a.Append(")WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ");
            a.Append(") ON [PRIMARY] ");
            foreach (Entidades.Housing Housing in HousingLista)
            {
                a.Append("Insert #Housing" + SessionID + " values (" + Housing.Id + ", '");
                a.Append(Housing.Usuario.Id + "', '");
                a.Append(Housing.Descripcion + "', '");
                a.Append(Housing.Pais + "', '");
                a.Append(Housing.Provincia + "', '");
                a.Append(Housing.Localidad + "', '");
                a.Append(Housing.Direccion + "', '");
                a.Append(Housing.CodPost + "', '");
                a.Append(Housing.Contacto.Nombre + "', '");
                a.Append(Housing.Contacto.Email + "', '");
                a.Append(Housing.Contacto.Telefono + "', '");
                a.Append(Housing.Comentarios + "', '");
                a.Append(Housing.GoogleMap + "', ");
                a.Append(Housing.WF.Id + ", '");
                a.Append(Housing.Estado + "', ");
                a.Append(Housing.Ranking + ", '");
                a.Append(Housing.TipoDestacado + "', '");
                a.Append(Housing.Neighborhood + "', '");
                a.Append(Housing.ApproximateAddress + "', ");
                a.Append(Housing.SizeSgm + ", '");
                a.Append(Housing.TypePlace + "', '");
                a.Append(Housing.TypeRoom + "', '");
                a.Append(Housing.TypeBathroom + "', '");
                a.Append(Housing.HousingOffer + "', '");
                a.Append(Housing.SkypeAddress + "', '");
                a.Append(Housing.Moneda + "', ");
                a.Append(Housing.PricePerDay + ", ");
                a.Append(Housing.PricePerMonth + ", '");
                a.Append(Housing.PriceComment + "', '");
                a.Append(Housing.Servicios + "') ");
            }
            a.Append("select * ");
            a.Append("from (select top {0} ROW_NUMBER() OVER (ORDER BY {1}) as ROW_NUM, ");
            a.Append("IdHousing, IdUsuario, Descripcion, Pais, Provincia, Localidad, Direccion, CodPost, ");
            a.Append("NombreContacto, EmailContacto, TelefonoContacto, Comentarios, GoogleMap, IdWF, Estado, Ranking, TipoDestacado, ");
            a.Append("Neighborhood, ApproximateAddress, SizeSgm, TypePlace, TypeRoom, TypeBathroom, HousingOffer, SkypeAddress, Moneda, PricePerDay, PricePerMonth, PriceComment, Servicios ");
            a.Append("from #Housing" + SessionID + " ");
            a.Append("ORDER BY ROW_NUM) innerSelect WHERE ROW_NUM > {2} ");
            a.Append("DROP TABLE #Housing" + SessionID);
            if (OrderBy.Trim().ToUpper() == "DIRECCION" || OrderBy.Trim().ToUpper() == "DIRECCION DESC" || OrderBy.Trim().ToUpper() == "DIRECCION ASC")
            {
                OrderBy = "#Housing" + SessionID + "." + OrderBy;
            }
            if (OrderBy.Trim().ToUpper() == "PAIS" || OrderBy.Trim().ToUpper() == "PAIS DESC" || OrderBy.Trim().ToUpper() == "PAIS ASC")
            {
                OrderBy = "#Housing" + SessionID + "." + OrderBy;
            }
            string commandText = string.Format(a.ToString(), ((IndicePagina + 1) * TamañoPagina), OrderBy, (IndicePagina * TamañoPagina));
            DataTable dt = new DataTable();
            dt = (DataTable)Ejecutar(commandText.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
            List<Entidades.Housing> lista = new List<Entidades.Housing>();
            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Entidades.Housing Housing = new Entidades.Housing();
                    Housing.Id = Convert.ToInt32(dt.Rows[i]["IdHousing"].ToString());
                    Housing.Usuario.Id = dt.Rows[i]["IdUsuario"].ToString();
                    Housing.Descripcion = dt.Rows[i]["Descripcion"].ToString();
                    Housing.Pais = dt.Rows[i]["Pais"].ToString();
                    Housing.Provincia = dt.Rows[i]["Provincia"].ToString();
                    Housing.Localidad = dt.Rows[i]["Localidad"].ToString();
                    Housing.Direccion = dt.Rows[i]["Direccion"].ToString();
                    Housing.CodPost = dt.Rows[i]["CodPost"].ToString();
                    Housing.Contacto.Nombre = dt.Rows[i]["NombreContacto"].ToString();
                    Housing.Contacto.Email = dt.Rows[i]["EmailContacto"].ToString();
                    Housing.Contacto.Telefono = dt.Rows[i]["TelefonoContacto"].ToString();
                    Housing.Comentarios = dt.Rows[i]["Comentarios"].ToString();
                    Housing.GoogleMap = dt.Rows[i]["GoogleMap"].ToString();
                    Housing.WF.Id = Convert.ToInt32(dt.Rows[i]["IdWF"].ToString());
                    Housing.WF.Estado = dt.Rows[i]["Estado"].ToString();
                    Housing.Ranking = Convert.ToInt32(dt.Rows[i]["Ranking"].ToString());
                    Housing.TipoDestacado = dt.Rows[i]["TipoDestacado"].ToString();
                    Housing.Neighborhood = Convert.ToString(dt.Rows[i]["Neighborhood"]);
                    Housing.ApproximateAddress = Convert.ToString(dt.Rows[i]["ApproximateAddress"]);
                    Housing.SizeSgm = Convert.ToString(dt.Rows[i]["SizeSgm"]);
                    Housing.TypePlace = Convert.ToString(dt.Rows[i]["TypePlace"]);
                    Housing.TypeRoom = Convert.ToString(dt.Rows[i]["TypeRoom"]);
                    Housing.TypeBathroom = Convert.ToString(dt.Rows[i]["TypeBathroom"]);
                    Housing.HousingOffer = Convert.ToString(dt.Rows[i]["HousingOffer"]);
                    Housing.SkypeAddress = Convert.ToString(dt.Rows[i]["SkypeAddress"]);
                    Housing.Moneda = Convert.ToString(dt.Rows[i]["Moneda"]);
                    if (!(String.IsNullOrEmpty(dt.Rows[i]["PricePerDay"].ToString())))
                    {
                        Housing.PricePerDay = Convert.ToDecimal(dt.Rows[i]["PricePerDay"]);
                    }
                    if (!(String.IsNullOrEmpty(dt.Rows[i]["PricePerMonth"].ToString())))
                    {
                        Housing.PricePerMonth = Convert.ToDecimal(dt.Rows[i]["PricePerMonth"]);
                    }
                    Housing.PriceComment = Convert.ToString(dt.Rows[i]["PriceComment"]);
                    Housing.Servicios = Convert.ToString(dt.Rows[i]["Servicios"]);
                    Housing.NombreImagenPortada = "PortadaGral.jpg";
                    lista.Add(Housing);
                }
            }
            return lista;
        }
        public bool ComprobarDireccionHousing(string Direccion)
        {
            bool resp = false;
            StringBuilder a = new StringBuilder(string.Empty);
            a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
            a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
            a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
            a.Append("from Housing where ");
            a.Append("Direccion like '%" + Direccion + "%'");
            DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
            if (dt.Rows.Count != 0)
            {
                resp = true;
            }
            return resp;
        }
        public bool EstaReservado(int IdHousing)
        {
            bool resp = false;
            StringBuilder a = new StringBuilder(string.Empty);
            a.Append("select Housing.IdHousing, Housing.IdUsuario, Housing.Descripcion, Housing.Pais, Housing.Provincia, Housing.Localidad, Housing.Direccion, Housing.CodPost, ");
            a.Append("Housing.NombreContacto, Housing.EmailContacto, Housing.TelefonoContacto, Housing.Comentarios, Housing.GoogleMap, Housing.IdWF, Housing.Estado, Housing.Ranking, Housing.UltActualiz, Housing.TipoDestacado, ");
            a.Append("Housing.Neighborhood, Housing.ApproximateAddress, Housing.SizeSgm, Housing.TypePlace, Housing.TypeRoom, Housing.TypeBathroom, Housing.HousingOffer, Housing.SkypeAddress, Housing.Moneda, Housing.PricePerDay, Housing.PricePerMonth, Housing.PriceComment, Housing.Servicios ");
            a.Append("from Housing where ");
            a.Append("IdHousing = " + IdHousing + " and Estado = 'Reserved'");
            DataTable dt = (DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.NoAcepta, sesion.CnnStr);
            if (dt.Rows.Count != 0)
            {
                resp = true;
            }
            return resp;
        }
        public int Reservar(Entidades.Housing Housing, string Evento, string Estado)
        {
            int cant = 0;
            if (sesion.Usuario.Id != null)
            {
                StringBuilder a = new StringBuilder(string.Empty);
                a.Append("declare @Cantidad int ");
                a.AppendLine("select @Cantidad=Count(*) from Housing where IdHousing = " + Housing.Id + " and Estado = 'Active'");
                a.AppendLine("if @Cantidad > 0 ");
                a.AppendLine("begin ");
                a.AppendLine("update Housing set Estado='" + Estado + "' where IdHousing = " + Housing.Id);
                a.AppendLine("insert Log values (" + Housing.WF.Id.ToString() + ", getdate(), '" + sesion.Usuario.Id + "', 'Housing', '" + Evento + "', '" + Estado + "', '') ");
                a.AppendLine("end ");
                a.AppendLine("select @Cantidad ");
                cant = Convert.ToInt32(((DataTable)Ejecutar(a.ToString(), TipoRetorno.TB, Transaccion.Usa, sesion.CnnStr)).Rows[0][0]);
            }
            return cant;
        }
    }
}
